package com.shkco.org.service.impl;

import com.shkco.basic.service.impl.BaseServiceImpl;
import com.shkco.basic.util.PageList;
import com.shkco.org.domain.Department;
import com.shkco.org.mapper.DepartmentMapper;
import com.shkco.org.query.DepartmentQuery;
import com.shkco.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.findAll();
        Map<Long,Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if (department.getParent_id() == null){
                deptTree.add(department);
            } else {
                Long parent_id = department.getParent_id();
                Department dept = map.get(parent_id);
                dept.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
